home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / games / zmachine.zoo / io.c < prev    next >
C/C++ Source or Header  |  1990-05-07  |  10KB  |  545 lines

  1. /*
  2. *    @(#)io.c    2.24
  3. */
  4.  
  5. # ifdef OSK
  6. # include <modes.h>
  7. # endif
  8. # include "zmachine.h"
  9. # include "keys.h"
  10.  
  11. char    *iovers = "122400";
  12. int    pfile = -1;
  13. FILE    *fopen();
  14.  
  15. char *print_name = "protocol";
  16. int printer_width = 80;
  17.  
  18. /*****************************************************************************/
  19.  
  20. struct dev *init_dev(out, crlf, l, h, wrap)
  21. void (*out)(); void (*crlf)(); int l, h, wrap;
  22. {
  23.     register struct dev *d;
  24.  
  25.     if (d = (struct dev *)malloc(sizeof(struct dev) + l))
  26.     {
  27.         d->buffer = d->bp = (char *)d + sizeof(struct dev);
  28.         d->width = l;
  29.         d->height = h;
  30.         d->count = 0;
  31.         d->out_f = out;
  32.         d->crlf_f = crlf;
  33.         d->wrap = wrap;
  34.         return(d);
  35.     }
  36.     else
  37.         no_mem_error();
  38. }
  39.  
  40. void putc_dev(c, d)
  41. char c; register struct dev *d;
  42. {
  43.     register char *s, *p;
  44.  
  45.     if (c == '\n' || c == FLUSH)
  46.     {
  47.         (*d->out_f)(d->buffer, d->bp);
  48.         if (c != FLUSH && (d->count != d->width || !d->wrap))
  49.             (*d->crlf_f)();
  50.         
  51.         con_flush();
  52.  
  53.         d->count = 0;
  54.         d->bp = d->buffer;
  55.     }
  56.     else
  57.     {
  58.         if (d->width < d->count+1)
  59.         {
  60.             if (c == ' ')
  61.             {
  62.                 (*d->out_f)(d->buffer, d->bp);
  63.                 if (d->count != d->width || !d->wrap)
  64.                     (*d->crlf_f)();
  65.                 d->count = 0;
  66.                 d->bp = d->buffer;
  67.                 return;
  68.             }
  69.             else
  70.             {
  71.                 for(s = d->bp; --s > d->buffer;)
  72.                     if (*s == ' ')
  73.                         break;
  74.  
  75.                 if (s == d->buffer)
  76.                 {
  77.                     (*d->out_f)(d->buffer, d->bp);
  78.                     d->count = 0;
  79.                     d->bp = d->buffer;
  80.                 }
  81.                 else
  82.                 {
  83.                     (*d->out_f)(d->buffer, s);
  84.                     (*d->crlf_f)();
  85.                     for (d->count = 0, p = d->buffer;
  86.                              s < d->bp-1;
  87.                                 d->count++)
  88.                         *p++ = *++s;
  89.                     d->bp = p;
  90.                 }
  91.             }
  92.  
  93.         }
  94.         *(d->bp++) = c;
  95.         d->count++;
  96.     }
  97. }
  98.  
  99. void output_chr(c)
  100. char c;
  101. {
  102.     extern int use_line, in_status;
  103.     extern char *line;
  104.  
  105.     if (c == FLUSH)
  106.         putc_dev(c, screen);
  107.     else if (use_line)
  108.         *line++ = c;
  109.     else
  110.     {
  111.         if (c == 9)
  112.             c = ' ';
  113.  
  114.         putc_dev(c, screen);
  115.  
  116.         if (!in_status && (word_get(main_h->reserved5) & 1))
  117.             putc_dev(c, printer);
  118.     }
  119. }
  120.  
  121. void output_str(p)
  122. register char *p;
  123. {
  124.     while(*p)
  125.         output_chr(*p++);
  126. }
  127.  
  128. void output_status(room, score)
  129. char *room, *score;
  130. {
  131.     register char i;
  132.     register int t;
  133.     int x,y;
  134.  
  135.     storeXY(&x, &y);
  136.     cursorOFF();
  137.     gotoXY(0,0);
  138.     reverseON();
  139.     con_str1(room);
  140.  
  141.     t = screen->width - strlen(score);
  142.     for(i = strlen(room); i < t; i++)
  143.         con_chr(' ');
  144.  
  145.     con_str1(score);
  146.     reverseOFF();
  147.     cursorON();
  148.     gotoXY(x,y);
  149. }
  150.  
  151. /*****************************************************************************/
  152.  
  153. void more_test()
  154. {
  155.     char getc_ne();
  156.     extern int line_cnt, status_len;
  157.  
  158.     if ((screen->height - status_len) == ++line_cnt)
  159.     {
  160.         status();
  161.         con_str1("[More]");
  162.         con_flush();
  163.         con_getc();
  164.         con_str1("\r      \r");
  165.         line_cnt = 2;
  166.     }
  167. }
  168.  
  169. void scr_write(a,b)
  170. char *a, *b;
  171. {
  172.     extern int in_status;
  173.  
  174.     if (!in_status)
  175.         more_test();
  176.  
  177.     if (b > a)
  178.         con_str2(a,b);
  179. }
  180.  
  181. int prot_open()
  182. {
  183.     if (pfile == -1)
  184.     {
  185. #ifdef OSK
  186.         if ((pfile = creat(print_name, S_IREAD|S_IWRITE)) < 0)
  187. #else
  188.         if ((pfile = creat(print_name, 0666)) < 0)
  189. #endif
  190.         {
  191.             con_crlf();
  192.             con_str1("Can't open protokoll file");
  193.             con_crlf();
  194.             pfile = -2;
  195.         }
  196.     }
  197.     return(pfile >= 0);
  198. }
  199.     
  200. void prot_write(a,b)
  201. char *a, *b;
  202. {
  203.         if (prot_open() && (b > a))
  204.             write(pfile, a, b-a);
  205. }
  206.  
  207. void prot_crlf()
  208. {
  209.     if (prot_open())
  210. # if defined(unix) || defined(OSK)
  211.         write(pfile, "\n", 1);
  212. # else
  213.         write(pfile, "\r\n", 2);
  214. # endif
  215. }
  216.  
  217. /*****************************************************************************/
  218.  
  219. char fkeys[10][17];
  220.  
  221. int save_keys()
  222. {
  223.     extern int save_len;
  224.     return(write_saveb(save_len + 1, 170, fkeys));
  225. }
  226.  
  227. int restore_keys()
  228. {
  229.     extern int save_len;
  230.     return(read_saveb(save_len + 1, 170, fkeys));
  231. }
  232.  
  233. char *read_str(p, hist)
  234. char *p; struct hist_buf *hist;
  235. {
  236.     extern int line_cnt;
  237.     register char *q, *src, *dst;
  238.     char *retype;
  239.     char *end;
  240.     register int c;
  241.     register int i, e;
  242.     int x, y;
  243.  
  244.     static char fbuf[18];
  245.     static char fhmem[36];
  246.     static struct hist_buf fhist = {36, fhmem, fhmem};
  247.     static int imode=1;
  248.     static int init=1;
  249.     static int fprog = 0;
  250.  
  251.     if (init)
  252.     {
  253.         init = 0;
  254.         fhmem[0] = fhmem[1] = '\0';
  255.  
  256.         for(i = F01; i < F10; i++)
  257.             fkeys[i][0] = '\0';
  258.     }
  259.  
  260.     retype = NULL;
  261.     con_flush();
  262.  
  263.     (*p)--;            /* we need one char more (for '\0')    */
  264.     line_cnt = 1;
  265.     p[1] = '\0';
  266.     end = p + p[0] + 1;
  267.     q = p + 1;
  268.     while ((c = con_getc()) != RETURN)
  269.     {
  270.         switch(c)
  271.         {
  272.             case 0x1b:
  273.                 {
  274.                     extern save_gf();
  275.                     save_gf("dump.z");
  276.                 }
  277.                 break;
  278.  
  279.             case CUP:
  280.             case CDOWN:
  281.                 if (c == CUP)
  282.                 {
  283.                     if (!retype)
  284.                         retype = hist->undo;
  285.                     else if (*(retype + strlen(retype) + 1))
  286.                         retype += strlen(retype) + 1;
  287.                     else
  288.                         break;
  289.                 }
  290.                 else
  291.                 {
  292.                     if (!retype)
  293.                         retype = hist->undo;
  294.                     else if (retype != hist->hb)
  295.                         for (retype -= 1;
  296.                           retype != hist->hb &&
  297.                             retype[-1]; retype--)
  298.                             ;
  299.                     else
  300.                         break;
  301.                 }
  302.  
  303.             case RETYPE:
  304.                 if (!retype)
  305.                     retype = hist->undo;
  306.  
  307.                 /* go to start of input line */
  308.                 e = q - p - 1;
  309.                 cursorOFF();
  310.                 for(i = 0; i < e; i++)
  311.                     cursorLEFT();
  312.  
  313.                 /* delete to end of input line */
  314.                 storeXY(&x, &y);
  315.                 e = strlen(p+1);
  316.                 for (i = 0; i < e; i++)
  317.                     con_chr(' ');
  318.                 gotoXY(x, y);
  319.  
  320.                 /* copy old input line and print it */
  321.                 strncpy(p + 1, retype, *p);
  322.                 *(p + *p + 1) = '\0';
  323.                 con_str1(p + 1);
  324.                 cursorON();
  325.                 q = p + 1 + strlen(p + 1);
  326.                 break;
  327.  
  328.             case CLEFT:
  329.                 if (q > p + 1)
  330.                 {
  331.                     q--;
  332.                     cursorLEFT();
  333.                 }
  334.                 break;
  335.  
  336.             case CRIGHT:
  337.                 if (*q != '\0')
  338.                     con_chr(*q++);
  339.                 break;
  340.  
  341.             case START:
  342.                 e = q - p - 1;
  343.                 cursorOFF();
  344.                 for(i = 0; i < e; i++)
  345.                     cursorLEFT();
  346.                 cursorON();
  347.                 q = p + 1;
  348.                 break;
  349.  
  350.             case END:
  351.                 cursorOFF();
  352.                 con_str1(q);
  353.                 cursorON();
  354.                 q += strlen(q);
  355.                 break;
  356.  
  357.             case BACKDEL:
  358.                 if (q > p + 1)
  359.                 {
  360.                     cursorLEFT();
  361.                     storeXY(&x, &y);
  362.                     strcpy(q-1, q);
  363.                     q--;
  364.                     cursorOFF();
  365.                     con_str1(q);
  366.                     con_chr(' ');
  367.                     gotoXY(x, y);
  368.                     cursorON();
  369.                 }
  370.                 break;
  371.  
  372.             case FORWDEL:
  373.                 if (*q != '\0')
  374.                 {
  375.                     storeXY(&x, &y);
  376.                     strcpy(q, q + 1);
  377.                     cursorOFF();
  378.                     con_str1(q);
  379.                     con_chr(' ');
  380.                     gotoXY(x, y);
  381.                     cursorON();
  382.                 }
  383.                 break;
  384.  
  385.             case KILL:
  386.                 if (*q != '\0')
  387.                 {
  388.                     storeXY(&x, &y);
  389.                     cursorOFF();
  390.                     e = strlen(q);
  391.                     for (i = 0; i < e; i++)
  392.                         con_chr(' ');
  393.                     *q = '\0';
  394.                     gotoXY(x, y);
  395.                     cursorON();
  396.                 }
  397.                 break;
  398.  
  399.             case INSERT:
  400.                 imode = !imode;
  401.                 break;
  402.  
  403.             case FPROG:
  404.                 if(!fprog)
  405.                 {
  406.                     fprog = 1;
  407.                     cursorOFF();
  408.                     storeXY(&x, &y);
  409.                     gotoXY(0, 0);
  410.                     reverseON();
  411.                     for (i = 0; i < screen->width; i++)
  412.                         con_chr(' ');
  413.                     gotoXY(0, 0);
  414.                     con_str1("Press function key: ");
  415.                     cursorON();
  416.                     con_flush();
  417.                     do
  418.                         c = con_getc();
  419.                     while(c < FSTART);
  420.                     cursorOFF();
  421.                     con_str1("\r                    \r");
  422.                     con_str1("Enter text: ");
  423.                     fbuf[0] = 17;
  424.                     cursorON();
  425.                     con_flush();
  426.                     read_str(fbuf, &fhist);
  427.                     cursorOFF();
  428.                     status();
  429.                     gotoXY(x, y);
  430.                     cursorON();
  431.                     strcpy(fkeys[c - FSTART], fbuf + 1);
  432.                     fprog = 0;
  433.                 }
  434.                 break;
  435.  
  436.             default:
  437.                 if (strlen(p + 1) < p[0] || (*q && !imode))
  438.                 {
  439.                     if (c >= ' ' && c < 0x80)
  440.                     {
  441.                         if (imode)
  442.                         {
  443.                             for (src = q + strlen(q),
  444.                                 dst = q + strlen(q) + 1;
  445.                                 src >= q;
  446.                                 *dst-- = *src--)
  447.                                 ;
  448.                             *q = c;
  449.                             cursorOFF();
  450.                             con_str1(q++);
  451.                             e = strlen(q);
  452.                             for(i = 0; i < e; i++)
  453.                                 cursorLEFT();
  454.                             cursorON();
  455.                         }
  456.                         else
  457.                         {
  458.                             if (!*q)
  459.                                 *(q+1) = '\0';
  460.                             *q++ = c;
  461.                             con_chr(c);
  462.                         }
  463.                     }
  464.                     else if (c >= FSTART)
  465.                     {
  466.                         if (!*q)
  467.                         {
  468.                             strncpy(q, fkeys[c - FSTART], end - q);
  469.                             cursorOFF();
  470.                             con_str1(q);
  471.                             cursorON();
  472.                             q += strlen(q);
  473.                         }
  474.                         else if (imode)
  475.